package com.zjl.litemall.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zjl.litemall.admin.vo.RegionVo;
import com.zjl.litemall.core.util.ResponseUtil;
import com.zjl.litemall.db.domain.LitemallRegion;
import com.zjl.litemall.db.service.LitemallRegionService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cat
 * @description
 * @date 2022/3/20 下午12:35
 */
@RestController
@RequestMapping("/admin/region")
@Api(tags = "商场管理")
public class AdminRegionController {

    @Autowired
    private LitemallRegionService regionService;

    @GetMapping("/list")
    public Object list() {
        List<RegionVo> list = new ArrayList<>();

        List<LitemallRegion> regionList = regionService.list();
        // 1省
        // 2市
        // 3县区
        List<LitemallRegion> regionProvienceList1 = regionList.stream().filter(e -> e.getType() == 1).collect(Collectors.toList());
        List<LitemallRegion> regionProvienceList2 = regionList.stream().filter(e -> e.getType() == 2).collect(Collectors.toList());
        List<LitemallRegion> regionProvienceList3 = regionList.stream().filter(e -> e.getType() == 3).collect(Collectors.toList());

        // TODO List<LitemallRegion>转List<RegionVo>
        for (LitemallRegion litemallRegion : regionProvienceList1) {
            RegionVo regionVo = new RegionVo();
            BeanUtil.copyProperties(litemallRegion,regionVo);

            // children设置
            List<LitemallRegion> regionList2 = regionProvienceList2.stream().filter(e -> e.getPid().equals(litemallRegion.getId())).collect(Collectors.toList());
            //regionVo.setChildren(regionList2);
            list.add(regionVo);
        }
        //BeanUtil.copyProperties(regionProvienceList1,list);
        return ResponseUtil.okList(list);
    }

    // 泛型？
    public static Object listBeanCopy(List<?> source, List<?> target) {
        for (Object o : source) {

        }
        return new Object();
    }
}
